package reu.model;

import java.util.LinkedList; 
import java.util.Scanner;

public class SigList {
	public final int ROWS = 26;
	private LinkedList<Entry>[] entries;
	private final int LOWER_ROOT = 97;
	private final int UPPER_ROOT = 65;
	
	@SuppressWarnings("unchecked")
	public SigList(){
		entries = (LinkedList<Entry>[])new LinkedList[ROWS];
	}
	
	public int add(Entry insert){
		int entries_index = -1;
		int listSize = 0;
        char ch = insert.getLastName().charAt(0);
        entries_index = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	entries_index = ch - LOWER_ROOT;
        }
		//instantiate the new entry
		listSize = entries[entries_index].size();
		if(listSize == 0){
			//add to front of list
			entries[entries_index].add(insert);
			entries_index = 0;
		}
		else{
			//list isn't empty
			if(find(insert,entries_index) < 0 ){
				//get the size of the row
				int rSize = entries[entries_index].size();
				//if the list has entries, add the new entry to the first
				//index where the new entry is less than
				for(int i = 0; i < rSize; i++){
					if((entries[entries_index].get(i)).compare(insert) > 0 )
					{
						entries[entries_index].add(i, insert);
						entries_index = i;
						break;
					}
					else if(i == rSize - 1){
						entries[entries_index].add(insert);
						entries_index = rSize;
					}
				}
			}
			//else it returns a negative value for the entry was in the list
		}
		return entries_index;
	}
	
	public boolean remove(String fname, String mname, String lname){
		int result = -1;
        char ch = lname.charAt(0);
        int lRow = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	lRow = ch - LOWER_ROOT;
        }
		Entry old = new Entry(fname,mname,lname);
		result = find(old, lRow);
		if(result >= 0){
	        entries[lRow].remove(result);
			return true;
		}
		return false;
		
	}
	
	public boolean edit(String fname, String mname, String lname){
        Entry temporary = new Entry(fname, mname, lname);
        //Determine what row to reference in the list
        char ch = lname.charAt(0);
        int entries_index = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	entries_index = ch - LOWER_ROOT;
        }
        
        int tempReturnVal = find(temporary,entries_index);
        Entry entryToEdit = entries[entries_index].get(tempReturnVal);
        Scanner userInput = new Scanner(System.in);
        System.out.print("What field would you like to edit Name (N) or Info (I)?");
        String fieldToEdit = userInput.nextLine();
        char firstCh = fieldToEdit.charAt(0);
        if(firstCh == 'N' || firstCh == 'n' ){
        	System.out.print("Which name woulf you like to edit First (F), Middle (M), or Last (L)");
        	String nameToEdit = userInput.nextLine();
        	char nameCh = nameToEdit.charAt(0);
        	String nameTemp = "";
        	if(nameCh == 'F'|| nameCh == 'f' ){
        		System.out.println("Current first name: " + entryToEdit.getFirstName());
        		System.out.print("New first name: ");
        		nameTemp = userInput.nextLine();
        		entryToEdit.setFirstName(nameTemp);
        		return true;
        	}
        	else if(nameCh == 'M'|| nameCh == 'm' ){
        		System.out.println("Current middle name: " + entryToEdit.getMidName());
        		System.out.print("New middle name: ");
        		nameTemp = userInput.nextLine();
        		entryToEdit.setMidName(nameTemp);
        		return true;
        	}
        	else if(nameCh == 'L'|| nameCh == 'l' ){
        		System.out.println("Current last name: " + entryToEdit.getLastName());
        		System.out.print("New last name: ");
        		nameTemp = userInput.nextLine();
        		entryToEdit.setLastName(nameTemp);
        		return true;
        	}
        }
        
        if(firstCh == 'I' || firstCh == 'i'){
        	System.out.print("Would you like to edit an award (E) or add an award (A)?");
        	String infoToEdit = userInput.nextLine();
        	char infoCh = infoToEdit.charAt(0);
        	if(infoCh == 'E' || infoCh == 'e'){
	        	System.out.println("Indexed list of awards");
	        	System.out.println("--------------------");
	        	int index = 0;
	        	for(String s : entryToEdit.info){
	        		index++;
	        		System.out.println("" + index + "  " + s); 
	        	}
	        		
	        	System.out.print("Which index would you like to edit? ");
	        	index = userInput.nextInt() - 1;
	        	String information = entryToEdit.info.get(index);
	        	String infolink = entryToEdit.infoLink.get(index);
	        	System.out.println("Current award info: " + information);
	        	System.out.print("New award  info: ");
	        	information = userInput.nextLine();
	        	entryToEdit.info.set(index, information);
	        	System.out.print("Would you like to edit the award hyperlink (y/n)? ");
	        	char input = userInput.nextLine().charAt(0);
	        	if(input == 'Y' || input == 'y'){
	        		System.out.println("Current award info link: " + infolink);
	        		System.out.print("New award info link: ");
	        		infolink = userInput.nextLine();
	        		entryToEdit.infoLink.set(index, infolink);
	        	}
	        	return true;
        	}
        	else if(infoCh == 'A' || infoCh == 'a'){
        		System.out.println("Indexed list of awards");
	        	System.out.println("--------------------");
	        	int index = 0;
	        	for(String s : entryToEdit.info){
	        		index++;
	        		System.out.println("" + index + "  " + s); 
	        	}
	        	System.out.println();
        		System.out.print("Name of the award to add: ");
        		String information = userInput.nextLine();
        		String infolink = "";
        		entryToEdit.info.add(information);
	        	System.out.print("Would you like to add a hyperlink (y/n)? ");
	        	char input = userInput.nextLine().charAt(0);
	        	if(input == 'Y' || input == 'y'){
	        		System.out.print("New award info link: ");
	        		infolink = userInput.nextLine();
	        	}
	        	entryToEdit.infoLink.add(infolink);
	        	index = 0;
	        	for(String s : entryToEdit.info){
	        		index++;
	        		System.out.println("" + index + "  " + s); 
	        	}
        	}
        }
		return false;
     
	
	}
	
	public int find(Entry e, int row){
		int result = -1;
		int rSize = entries[row].size();
		//if the list has entries
		for(int i = 0; i < rSize; i++){
			if((entries[row].get(i)).compare(e) == 0 )
			{
				 result = i;
				 break;
			}
		}
		return result;
	}

}
